import logging

from config import BASE_URL


class ReLoginApi(object):
    def __init__(self, session):
        self.session = session

    def get_img_verify_code(self, r):
        url = BASE_URL + f'/common/public/verifycode1/{r}'
        resp = self.session.get(url)
        print(f'获取图片验证码的状态码 = {resp.status_code}')
        logging.info(f'获取图片验证码的状态码 = {resp.status_code}')
        return resp

    def get_phone_verify_code(self, phone_v, imgVerifyCode_v, type_v='reg'):
        url = BASE_URL + f'/member/public/sendSms'
        info = {"phone": phone_v,
                "imgVerifyCode": imgVerifyCode_v,
                'type': type_v}
        resp = self.session.post(url, data=info)
        print(f'注册获取注册短信验证码成功（所有必填参数必填）响应数据 = {resp.json()}')
        logging.info(f'注册获取注册短信验证码成功（所有必填参数必填）响应数据 = {resp.json()}')
        return resp

    def user_register(self, phone_v, pwd_v, verifyCode_v, phoneCode_v, dyServer_v='on', invitePhone_v=None):
        url = BASE_URL + f'/member/public/reg'
        info = {"phone": phone_v, "password": pwd_v,
                "verifycode": verifyCode_v, "phone_code": phoneCode_v,
                "dy_server": dyServer_v}
        resp = self.session.post(url, data=info)
        print(f'注册成功（仅必填参数（dy_server=on)）的响应数据 = {resp.json()}')
        logging.info(f'注册成功（仅必填参数（dy_server=on)）的响应数据 = {resp.json()}')
        return resp

    def user_login(self, username_v, pwd_v):
        url = BASE_URL + '/member/public/login'
        info = {"keywords": username_v, "password": pwd_v}
        resp = self.session.post(url, data=info)
        print(f'登录成功（所有项必填）的响应数据 = {resp.json()}')
        logging.info(f'登录成功（所有项必填）的响应数据 = {resp.json()}')
        return resp
